// @Author EthanScriptOn
// @Desc
package config_freighter

import (
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/initialization"
	"gitee.com/fatzeng/srf_switch_config/config_context"
	"gitee.com/fatzeng/srf_switch_config/config_define"
	"gitee.com/fatzeng/srf_switch_config/config_parser"
	"gitee.com/fatzeng/srf_switch_protocol/protocol/log"
	"math"
)

const FreightTagEnum = "mapping"

var freightTag = func() *initialization.OnceInitializer {
	initializerInstance := initialization.GenerateOnceInitializer(config_define.ParserPositionTag, config_define.FreightTagInitTag)
	if err := initializerInstance.Set(math.MaxInt, func() (result interface{}, err error) {
		return FreightTagEnum, nil
	}); err != nil {
		panic(err)
	}
	return initializerInstance
}()

func GetFreightTag() (string, error) {
	freightTagInstance, err := freightTag.Get()
	if err != nil {
		return "", err
	}
	freightTagConversionInstance, ok := freightTagInstance.(string)
	if !ok {
		return "", errors.New("freightTag type error need string")
	}
	return freightTagConversionInstance, nil
}

type FundamentalFreighterConfigurator struct {
	config_parser.ResourceFreighter
}

func GenerateFundamentalFreighterConfigurator() *FundamentalFreighterConfigurator {
	return new(FundamentalFreighterConfigurator)
}

func (f *FundamentalFreighterConfigurator) Freight(ctx *config_context.ConfigInitializationContext, resolvedData interface{}) error {
	mergedData := resolvedData.(map[string]interface{})
	freightTagEnum, err := GetFreightTag()
	if err != nil {
		return err
	}
	for _, cacheStruct := range ctx.GetFieldCache() {
		if err := cacheStruct.TransformTaggedInformation(mergedData, freightTagEnum); err != nil {
			return err
		}
	}
	return nil
}

func (f *FundamentalFreighterConfigurator) FreightPrepare(ctx *config_context.ConfigInitializationContext) (err error) {
	log.Logger().Info("You can implement this function to perform some preprocessing operations")
	return
}

func (f *FundamentalFreighterConfigurator) FreightDoMore(ctx *config_context.ConfigInitializationContext) (err error) {
	log.Logger().Info("You can implement this function to perform some preprocessing operations")
	return
}
